package com.qfedu.user.controller;

import com.qfedu.common.annotation.NoLoginCheck;
import com.qfedu.common.entity.User;
import com.qfedu.common.interceptor.ThreadLocalCommon;
import com.qfedu.common.utils.JwtUtils;
import com.qfedu.common.vo.R;
import com.qfedu.user.service.UserService;
import com.qfedu.user.vo.UserVO;
import io.jsonwebtoken.Claims;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/app/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/my")
    public R<UserVO> my(HttpServletRequest request) {
        String jwt = request.getHeader("Token");
        Claims claims = JwtUtils.parseJWT(jwt);
        Object uid = claims.get("uid");
        UserVO my = userService.my(Integer.valueOf(uid.toString()));
        return R.ok(my);
    }

    @GetMapping("/info")
    public R<User> userInfo(HttpServletRequest req) {
        String token = req.getHeader("token");
        userService.userInfo(token);
        return R.ok(new User());
    }

    @NoLoginCheck
    @GetMapping("/time")
    public R testTime() {
        Long aLong = ThreadLocalCommon.threadLocal.get();
        System.out.println("---------------:" + aLong);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return R.ok("haha");
    }

    @NoLoginCheck
    @GetMapping("/time2")
    public R testTime2() {
        System.out.println("haha");
        return R.ok("haha");
    }
}
